﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Web;
using System.Web.Mvc;
using GTAuto.DAL.DAL;
using GTAuto.DAL.Entities;
using System.Linq;
using GTAuto.DAL.Enums;
using GTAuto.Site.BackEnd.ApCode.Filters;


namespace GTAuto.Site.BackEnd.Controllers
{

     [AutoSection]
    public class BrandModelsController : Controller
    {
        //
        // GET: /BrandModels/

        public ActionResult ModelsByBrend(int id)
        {
            int count;

            ViewBag.BrandName = DALBrands.Singleton.Get(id).Name;

            List<GTModel> models = DALModels.Singleton.GetByBrand(id, 1, 100000, out count);
            Session["CurBrend"] = id;
            return View(models);
        }

        public ActionResult Edit(int? id)
        {

            if (Session["CurBrend"] == null)
                return RedirectToAction("Index", "Home");


            var currentBrend = (int)Session["CurBrend"];
            var brend = DALBrands.Singleton.Get(currentBrend);
            ViewBag.Brend = brend;


            int count;
            ViewBag.brands = new SelectList(DALBrands.Singleton.GetPaged((BrandTypeEnum)brend.Type, 1, 10000, out count), "Id", "Name");

            GTModel gtModel;
            if (id == null)
            {
                gtModel = new GTModel();
                gtModel.BrandId = currentBrend;
            }
            else
            {
                gtModel = DALModels.Singleton.Get((int)id);
            }


            var props = new List<GTModelModificationProperty>();
            if (id != null)
            {
                props = DALModelModificationProperty.Singleton.GetByModel((int)id);
            }
            ViewBag.Props = props;


            return View(gtModel);
        }

        [HttpPost]
        public ActionResult Edit(GTModel gtModel, FormCollection formCollection, HttpPostedFileBase Logo, HttpPostedFileBase LogoSmal)
        {

            if (Session["CurBrend"] == null)
                return RedirectToAction("Index", "Home");




            if (Logo != null)
            {

                string filename = Guid.NewGuid().ToString() + "." + Logo.FileName.Split('.')[1];

                string filePath = Path.Combine(HttpContext.Server.MapPath(Url.Content(@"/Uploads/BrendLogos/"))
               , Path.GetFileName(filename));

                Logo.SaveAs(filePath);
                gtModel.Logo = filename;
            }

            if (LogoSmal != null)
            {
                string filename = Guid.NewGuid().ToString() + "." + LogoSmal.FileName.Split('.')[1];

                string filePath = Path.Combine(HttpContext.Server.MapPath(Url.Content(@"/Uploads/BrendLogos/"))
               , Path.GetFileName(filename));
                LogoSmal.SaveAs(filePath);
                gtModel.LogoSmal = filename;
            }


            var currentBrend = (int)Session["CurBrend"];
            var brend = DALBrands.Singleton.Get(currentBrend);
            ViewBag.Brend = brend;

            //ViewBag.CategoryName = DALPropertyCategory.Singleton.Get(id).Name;
            int count;
            ViewBag.brands = new SelectList(DALBrands.Singleton.GetPaged((BrandTypeEnum)brend.Type, 1, 10000, out count), "Id", "Name");

            var Error = new List<string>();

            if (string.IsNullOrEmpty(gtModel.Name))
                Error.Add("Название не может быть пустым");

            ViewBag.Errs = Error;


            if (Error.Count == 0)
            {
                if (gtModel.Id == 0)
                {

                    gtModel = DALModels.Singleton.Add(gtModel);
                }
                else
                {
                    var oldModel = DALModels.Singleton.Get(gtModel.Id);


                    if (string.IsNullOrEmpty(gtModel.Logo))
                        gtModel.Logo = oldModel.Logo;

                    if (string.IsNullOrEmpty(gtModel.LogoSmal))
                        gtModel.LogoSmal = oldModel.LogoSmal;

                    DALModels.Singleton.Update(gtModel);
                }

                var props = DALModificationProperty.Singleton.GetAll();

                foreach (var prop in props)
                {
                    if (formCollection["prop_" + prop.Id.ToString()] != null)
                    {
                        if (DALModelModificationProperty.Singleton.Get(gtModel.Id, prop.Id) == null)
                        {
                            var property = new GTModelModificationProperty();
                            property.ModelId = gtModel.Id;
                            property.PropertyId = prop.Id;
                            DALModelModificationProperty.Singleton.Add(property);
                        }
                    }
                    else
                    {
                        if (DALModelModificationProperty.Singleton.Get(gtModel.Id, prop.Id) != null)
                        {
                            DALModelModificationProperty.Singleton.DeleteByModelAndProp(gtModel.Id, prop.Id);
                            var lst = DALModelModification.Singleton.GetAll(gtModel.Id);
                            foreach (var gtModelModification in lst)
                            {
                                DALModificationPropertyValue.Singleton.Delete(gtModelModification.Id, prop.Id);
                            }
                        }
                    }
                }

                return RedirectToAction("ModelsByBrend", new { @id = gtModel.BrandId });
            }
            else
            {
                gtModel.Logo = "";
                gtModel.LogoSmal = "";
            }

            var propss = new List<GTModelModificationProperty>();
            propss = DALModelModificationProperty.Singleton.GetByModel(gtModel.Id);
            ViewBag.Props = propss;

            return View(gtModel);
        }

        public ActionResult Delete(int id)
        {
            var modificationProperty = DALModels.Singleton.Get((int)id);
            int catID = modificationProperty.BrandId;
            DALModels.Singleton.Delete(id);
            return RedirectToAction("ModelsByBrend", new { @id = catID });
        }


    }
}
